// @flow
import React, { Component } from 'react'
import styles from './Point.css'
import { Button, Modal, message } from 'antd'
import PointModal from './PointModal'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import PointGrid from './PointGrid'
type Props = {
  location: Object,
  pointData: Array<Object>,
  refreshPoint: Function,
  getPointVars: Function,
  getVarData: FUnction
}
type States = {
  visiblePoint: boolean,
  confirmLoading: boolean
}
class Point extends Component {
  props: Props
  state: States
  showPointModal: Function
  handlePointOk: Function
  handlePointCancel: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      visiblePoint: false,
      confirmLoading: false
    }
    this.showPointModal = this.showPointModal.bind(this)
    this.handlePointOk = this.handlePointOk.bind(this)
    this.handlePointCancel = this.handlePointCancel.bind(this)
  }
  componentDidMount () {
    // const { location } = this.props
    // universalFetch(`${__SERVICE_API__}device/templates/devices/${location.state.recordId}/points`)
    // .then()

  }
  showPointModal () {
    this.setState({
      visiblePoint: true
    })
  }

  handlePointOk = () => {
    const form = this.refs.pointModal.getForm()
    form.validateFields((err, fieldsValue) => {
      if (err) {
        return false
      }
      this.setState({
        confirmLoading: true
      })
      const { refreshPoint } = this.props
      const values = {
        ...fieldsValue
      }

      universalFetch(`${__SERVICE_API__}device/templates/monitors/points`, {
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        method: 'POST',
        body: JSON.stringify(values)
      })
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        message.success('测点添加成功')
        refreshPoint()
        this.setState({
          visiblePoint: false,
          confirmLoading: false
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
        this.setState({
          confirmLoading: false
        })
        message.error('测点添加失败: ' + error.message, 10)
      })
    })
  }

  handlePointCancel () {
    this.setState({
      visiblePoint: false
    })
  }
  render () {
    const { pointData, getPointVars, refreshPoint, getVarData } = this.props
    return (
      <div className={styles['main']}>
        <Button type='primary'
          onClick={() => this.showPointModal()}>添加测点</Button>
        <PointGrid {...{
          rowData: pointData,
          getPointVars,
          refreshPoint,
          getVarData
        }}
          />
        <Modal
          title='添加测点'
          visible={this.state.visiblePoint}
          onOk={this.handlePointOk}
          confirmLoading={this.state.confirmLoading}
          onCancel={this.handlePointCancel}
          >
          <PointModal ref='pointModal' />
        </Modal>
      </div>
    )
  }
}

export default Point
